package com.cy.controller;

import com.cy.pojo.GoodsSingle;
import com.cy.pojo.UserBean;
import com.cy.service.Showservice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;

@Controller
public class SignInController {
    @Autowired
    private Showservice sser;

    @RequestMapping("/ShowCar.html")
    public String ShowCar(){
        return "ShowCar";
    }

    @RequestMapping("/visitor.html")
    public String visitor(HttpSession session) {
        session.setAttribute("user","游客");
        return "redirect:/show.html";
    }

    @RequestMapping("/denglu.html")
    public String denglu() {
        return "denglu";
    }

    @RequestMapping("/zhuce.html")
    public String zhuce() {
        return "zhuce";
    }

    @RequestMapping("/show.html")
    public String show() {
        return "show";
    }

    @RequestMapping("/Admi.html")
    public String Admi() {
        return "Admi";
    }

    @RequestMapping("/SellerAdmi.html")
    public String SellerAdmi() {
        return "SellerAdmi";
    }


    @RequestMapping("/servlet.treatDengLu")
    public String treatDengLu(@RequestParam("name") String name, @RequestParam("pwd")String password, Model model, HttpSession session){
        System.out.println(session.getId());


        if(StringUtils.isEmpty(name)||StringUtils.isEmpty(password)){
            model.addAttribute("msg","用户名或密码不能为空");
            return "denglu";
        }else if(name.equals("陈毅")&&password.equals("20202671")){
            return "redirect:/Admi.html";
        }
        else {
            UserBean userBean = sser.selectXingxi(name, password);
            if(userBean==null){
                model.addAttribute("msg","用户名或密码错误");
                return "denglu";
            }else{
                ArrayList <GoodsSingle>buylist = new ArrayList<>();
                session.setAttribute("user",name);
                String id = session.getId();
                session.setAttribute(id+"shopCar",buylist);
                return "redirect:/show.html";
            }
        }
    }

    @RequestMapping("/servlet.treatApply")
    public String treatApply(@RequestParam("name") String name, @RequestParam("pwd")String password,@RequestParam("pwd1")String password1, Model model){
        sser.insertZhuce(name,password,password1);
        return "redirect:/zhuce.html";
    }
}
